"""
@Author  : 康帅
@Time    : 2021/3/24 15:08
@Function: 构建模式：玩具房子搭建v2.0
"""

from abc import ABCMeta, abstractmethod


class Toy(metaclass=ABCMeta):
    """ 玩具 """

    def __init__(self, name):
        self._name = name
        self._components = []

    def get_name(self):
        return self._name

    def add_component(self, component, count=1, unit="个"):
        self._components.append([component, count, unit])
        print("%s 增加了 %d %s%s" % (self._name, count, unit, component))

    @abstractmethod
    def feature(self):
        pass


class Car(Toy):
    """ 小车 """

    def feature(self):
        print(f"我是{self._name},可以快速奔跑")


class Manor(Toy):
    """ 庄园 """

    def feature(self):
        print(f"我是{self._name},可以观赏也可以游玩")


class ToyBuilder(metaclass=ABCMeta):
    """ 玩具构建者 """

    @abstractmethod
    def build_product(self):
        pass


class CarBuild(ToyBuilder):
    """
    车构建类:
        构建模式的核心类，负责产品的构建过程
    """

    def build_product(self):
        car = Car("迷你小车")
        print(f"正在构建{car.get_name()}")
        car.add_component("轮子", 4)
        car.add_component("车身", 1)
        car.add_component("发动机", 1)
        car.add_component("方向盘")
        return car


class ManorBuild(ToyBuilder):
    """
    花园构建类:
        构建模式的核心类，负责产品的构建过程
    """

    def build_product(self):
        manor = Manor("涛涛小庄园")
        print(f"正在构建{manor.get_name()}")
        manor.add_component("客厅", 1, "间")
        manor.add_component("卧室", 2, "间")
        manor.add_component("书房", 1, "间")
        manor.add_component("厨房", 1, "间")
        manor.add_component("花园", 1)
        manor.add_component("围墙", 1, "堵")
        return manor


class BuilderMgr:
    """
    构建类的管理类:
        构建的管理类，负责管理每一种产品的创建数量和创建顺序
    """

    def __init__(self):
        self.__car_builder = CarBuild()
        self.__manor_builder = ManorBuild()

    def builder_car(self, num):
        count = 0
        products = []
        while count < num:
            car = self.__car_builder.build_product()
            products.append(car)
            count += 1
            print(f"建造完成第{count}辆{car.get_name()}")
        return products

    def builder_manor(self, num):
        count = 0
        products = []
        while count < num:
            manor = self.__manor_builder.build_product()
            products.append(manor)
            count += 1
            print(f"建造完成第{count}个{manor.get_name()}")
        return products


if __name__ == '__main__':
    builder_mgr = BuilderMgr()
    builder_mgr.builder_manor(2)
    print()

    builder_mgr.builder_car(4)
